﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace ServBus.ListForm2010.Common.WFXSNOperator
{
    public abstract class XSNFile
    {
        #region Properties

        public string FileName { get; set; }

        public XmlDocument XmlDoc { get; protected set; }

        public string TemplateName { get; set; }

        public XPathNavigator XPathNav { get; protected set; }

        public XmlNamespaceManager XmlNSManager { get; protected set; }

        public string SavePath { get; set; }

        public bool SaveToLocal { get; set; }

        public string InnerXPath { get; set; }

        #endregion

        protected bool InitialXmlDocument(string p_TemplateDirectory)
        {
            bool rStatus = false;

            try
            {
                //Get Template XML
                XmlDoc = new XmlDocument();
                XmlDoc.LoadXml(File.ReadAllText(p_TemplateDirectory + TemplateName, Encoding.UTF8));

                //Initial XMLNamespaceManager
                XmlNSManager = new XmlNamespaceManager(XmlDoc.NameTable);
                XmlNSManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
                XmlNSManager.AddNamespace("xsf", "http://schemas.microsoft.com/office/infopath/2003/solutionDefinition");
                XmlNSManager.AddNamespace("ma",
                    "http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes");
                XmlNSManager.AddNamespace("my",
                    "http://tempuri.org/");
                XmlNSManager.AddNamespace("pc", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                XmlNSManager.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
                XmlNSManager.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003");
                XmlNSManager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
                XmlNSManager.AddNamespace("tns", "http://tempuri.org/");

                //Initial XPathNavigator
                XPathNav = XmlDoc.CreateNavigator();
                rStatus = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return rStatus;
        }

        public bool CheckNode(string p_XPath)
        {
            bool rStatus = false;

            try
            {
                if (!string.IsNullOrEmpty(p_XPath))
                {
                    XPathNav.MoveToRoot();
                    XPathNavigator xNav = XPathNav.SelectSingleNode(p_XPath, XmlNSManager);
                    if (xNav != null)
                    {
                        rStatus = true;
                    }
                }
            }
            catch (Exception)
            {
                rStatus = false;
            }

            return rStatus;
        }

        public bool InsertNode(string p_XPath, string p_NodeStr)
        {
            bool rStatus = false;

            try
            {
                if (!string.IsNullOrEmpty(p_NodeStr))
                {
                    XPathNav.MoveToRoot();
                    XPathNavigator xNav = XPathNav.SelectSingleNode(p_XPath, XmlNSManager);
                    if (xNav != null) xNav.AppendChild(p_NodeStr);
                    rStatus = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                rStatus = false;
            }

            return rStatus;
        }

        public bool Save()
        {
            bool rStatus = false;
            if (SaveToLocal)
            {
                XmlDoc.Save(SavePath);
                rStatus = true;
            }
            return rStatus;
        }

        public virtual void AppendFieldInfoMainDef(XFieldInfo p_FieldInfo)
        {
        }

        public virtual void AppendFieldInfoSubDef(XListInfo p_XListInfo)
        {
        }
    }
}